package com.example.demo2.controller;

import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.AdminDO;
import com.example.demo2.pojo.vo.AdminVO;
import com.example.demo2.pojo.vo.SupplierVO;
import com.example.demo2.service.AdminService;
import com.example.demo2.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo2.utils.MD5Utils;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;

@Controller
public class AdminController {
    @Resource
    AdminService adminService;
    @RequestMapping("/user_list")
    public String admin(String name, @RequestParam(defaultValue = "1") Integer currentPage, Model model) throws Exception {
        AdminVO adminVO = new AdminVO();
        adminVO.setUsername(name);
        adminVO.setCurrentPage(currentPage);
        PageBean pageBean=adminService.listPage(adminVO);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("query", adminVO);
        return "user_list";
    }
    @RequestMapping("/user_list/add")
    @ResponseBody
    //添加
    public Result add(String username, String password )throws Exception{
        AdminVO adminVO=new AdminVO();
        adminVO.setUsername(username);
        adminVO.setPassword(MD5Utils.md5(password));
        adminVO.setIdentity("0");//0默认为普通用户身份
        return adminService.add(adminVO);
    }
    @RequestMapping("/user_list/delete/{id}")
    @ResponseBody
    //删除
    public Result deleteById(@PathVariable("id") Integer id)throws Exception{
        Result result = adminService.deleteById(id);
        Integer code = result.getCode();
        if (code==200){
            adminService.deleteById(id);
        }
        return result;
    }
    //更新
    @RequestMapping("/user_list/update")
    @ResponseBody
    public Result updateById(Integer id,String username2,String password2)throws Exception{
        AdminVO adminVO=new AdminVO();
        adminVO.setId(id);
        adminVO.setUsername(username2);
        adminVO.setPassword(MD5Utils.md5(password2));
        return adminService.updateById(adminVO);
    }
    @RequestMapping("/user_list/find/{id}")
    @ResponseBody
    //更新回显
    public Result findClass(@PathVariable("id")Integer id)throws Exception{
        return adminService.findById(id);
    }
}
